package edu.nf.aop.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

/**
 * @author wangl
 * @date 2021/9/24
 * Cglib动态代理工厂
 */
public class CglibAopProxy extends AbstractAopProxy {

    /**
     * 使用cglib动态代理
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T> T createProxy(Class<T> targetClass) {
        MethodInterceptor interceptor = (proxy, method, args, methodProxy) ->
                invoke(targetClass, method, args);
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetClass);
        enhancer.setCallback(interceptor);
        return (T) enhancer.create();
    }
}
